package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.shop.PayType;
import com.xishu.entity.shop.Shop;
import com.xishu.entity.shop.TakeOutSetting;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.*;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "外卖设定")
@RestController
public class TakeOutSettingController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(TakeOutSettingController.class);
    private CommonService commonService = CommonService.getInstance();

    @ApiOperation(value = "创建外卖设定")
    @PostMapping(value = ("/take/out/setting"), produces = ("application/json;charset=UTF-8"))
    public ResponseData create(@RequestBody TakeOutSetting takeOutSetting) throws Exception {
        VerifyUtil.verify(() -> takeOutSetting.getShopId() != null);
        VerifyUtil.verify(() -> takeOutSetting.getDefaultSetting() == null);
        ZhEnService.getInstance().dealZhEn(takeOutSetting);
        return commonService.createObject(takeOutSetting);
    }

    @ApiOperation(value = "修改外卖设定")
    @PutMapping(value = ("/take/out/setting/modify"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData modify(@RequestBody TakeOutSetting takeOutSetting) throws Exception {
        VerifyUtil.verify(() -> takeOutSetting.getId() != null);
        VerifyUtil.verify(() -> takeOutSetting.getDefaultSetting() == null);

        //默认的不能修改
        TakeOutSetting takeOutSettingInDb = commonService.findEntity(takeOutSetting.getId(), TakeOutSetting.class);
        VerifyUtil.verify(() -> !getBoolean(takeOutSettingInDb.getDefaultSetting()));

        return commonService.modifyObject(takeOutSetting);
    }

    @ApiOperation(value = "查询外卖设定")
    @PutMapping(value = ("/take/out/setting"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody TakeOutSetting takeOutSetting) throws Exception {
        //保证有一个默认的外卖设定
        if (takeOutSetting.getShopId() != null) {
            TakeOutSettingService.getInstance().findTakeAwaySetting(takeOutSetting.getShopId());
        }

        return commonService.searchResponse(takeOutSetting, (t) -> {
            Long shopId = t.getShopId();
            try {
                Shop shop = ShopService.getInstance().findShopById(shopId);
                t.setShopName_zh(shop.getName_zh());
                t.setShopName_en(shop.getName_en());

                if (t.getPayTypeId() != null) {
                    List<PayType> payTypeList = PayTypeService.getInstance().findPayTypeListCache(shopId);
                    PayType payType = PayTypeService.getInstance().filterPayType(payTypeList, t.getPayTypeId());
                    t.setPayTypeName_zh(payType.getName_zh());
                    t.setPayTypeName_en(payType.getName_en());
                }
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

    @ApiOperation(value = "删除外卖设定")
    @DeleteMapping(value = ("/take/out/setting"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData delete(@RequestBody TakeOutSetting takeOutSetting) throws Exception {
        //默认的不能删除
        TakeOutSetting takeOutSettingInDb = commonService.findEntity(takeOutSetting.getId(), TakeOutSetting.class);
        VerifyUtil.verify(() -> !getBoolean(takeOutSettingInDb.getDefaultSetting()));
        return commonService.delete(takeOutSetting);
    }

}
